package liuds.music.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.List;
import java.util.Map;

import liuds.music.R;

/**
 * Created by Administrator on 2018/5/31.
 */

public class SearchHistoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<Map<String,Object>> searchHistorys;
    private OnSearchHistoryItemClickListener onSearchHistoryItemClickListener;

    public SearchHistoryAdapter(Context context,List<Map<String,Object>> searchHistorys){
        this.context = context;
        this.searchHistorys = searchHistorys;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.search_history_item,null);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onSearchHistoryItemClickListener.onSearchHistoryItemClick(v);
            }
        });
        return new SearchHistoryViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        SearchHistoryViewHolder searchHistoryViewHolder = (SearchHistoryViewHolder)holder;
        searchHistoryViewHolder.title.setText(searchHistorys.get(position).get("title")+"");
        searchHistoryViewHolder.clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onSearchHistoryItemClickListener.onSearchHistoryItemMenuClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return searchHistorys.size();
    }

    private class SearchHistoryViewHolder extends RecyclerView.ViewHolder{

        private TextView title;
        private ImageButton clear;

        public SearchHistoryViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
            clear = itemView.findViewById(R.id.clear);
        }
    }

    public void setOnSearchHistoryItemClickListener(OnSearchHistoryItemClickListener onSearchHistoryItemClickListener) {
        this.onSearchHistoryItemClickListener = onSearchHistoryItemClickListener;
    }

    public interface OnSearchHistoryItemClickListener{
        void onSearchHistoryItemClick(View v);
        void onSearchHistoryItemMenuClick(int position);
    }
}
